﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Slja.Model.Project;
using Slja.DBUtility;
using System.Data;
using System.Data.SqlClient;

namespace Slja.DAL.Project
{
    public class RunningAccountDAL
    {
        // 未完成。。。
        public int RunAccoUpdate(RunAccoModifyModel model)
        {
            //更新数据
            string sqlfmt = "update [{0}] set {1}=@newval where {2}=@ID";
            string sql = string.Format(sqlfmt, model.TableName, model.Field, model.KeyName);
            SqlParameter[] parameters = new SqlParameter[2];
            (parameters[0] = new SqlParameter("@ID", SqlDbType.NVarChar, 20)).Value = model.KeyVal;
            (parameters[1] = new SqlParameter("@newval", SqlDbType.NVarChar, 100)).Value = model.NewVal;
            //保存日志
            StringBuilder logsql= new StringBuilder();
            logsql.Append("if(exists(select 1 from dbo.f_log where TableName=@tablname and PrjID=@prjid)) begin");
            logsql.Append(" update dbo.f_log set LogContent.modify('insert <lg dtm=\"2012-6-12 12:12:03\" userid=\"1\" username=\"liangning\" optype=\"2\" >");
            logsql.Append("<field></field>");
            logsql.Append("<oldval></oldval>");
            logsql.Append("<newval></newval>");
            logsql.Append("</lg> as last into (/data/)[1]')");
            logsql.Append(" where TableName=@tablename and PrjID=@ID");
            logsql.Append(" end else begin");
            logsql.Append(" insert into dbo.f_log(TableName,PrjID,LogContent) values(@table,@PrjID,'<data><lg dtm=\"2012-6-12 12:12:01\" userid=\"1\" username=\"liangning\" optype=\"2\" ></lg></data>')");
	        logsql.Append(" end");												

            int rowscount = DbHelperSQL.ExecuteSql(sql, parameters);
            return rowscount;
        }
        public int RunAccoInsert(string TableName,string jsonData)
        {
            //string sql = "insert into {0}";
            return 0;
        }
        /// <summary>
        /// 删除一条台账
        /// </summary>
        /// <param name="TableName">表名</param>
        /// <param name="ID">项目ID号</param>
        /// <returns></returns>
        public int RunAccountDel(string TableName, string ID)
        {
            string sql = "delete from ["+TableName+"] where ID=@ID";
            SqlParameter para ;
            (para = new SqlParameter("@ID", SqlDbType.NVarChar, 20)).Value = ID;
            int rowscount = DbHelperSQL.ExecuteSql(sql, para);
            return rowscount;
        }
    }
}
